package view;

import java.util.zip.DataFormatException;

import javax.swing.JTable;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import server.FirewallException;
import server.Rule;

public class RuleTable extends JTable
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2424212670113646511L;

	/* Column names */
	protected final static String columnNames[] = 
		{"Id", "IP", "Port", "Protocol", "Action"};
	
	/* Rows number */
	protected final static int rowNumber = 1;
	
	/* The table */
	protected static RuleTable currentRuleTable = null;
	
	public RuleTable()
	{
		super(rowNumber, columnNames.length);
		
		for(int i = 0; i < columnNames.length; i++)
		{
			setValueAt("", 0, i);
		}
		
		currentRuleTable = this;
	}
	
	public RuleTable(String[] rule)
	{
		super(rowNumber, columnNames.length);
		
		for(int i = 0; i < columnNames.length && i < rule.length; i++)
		{
			setValueAt(rule[i], 0, i);
		}
		
		currentRuleTable = this;
	}

	/**
	 * Createa and returns table header.
	 * @return the header of the table
	 */
	public JTableHeader getRulesTableHeader()
	{		
		TableColumn column = null;
		for (int i = 0; i < columnNames.length; i++)
		{
		    column = getColumnModel().getColumn(i);
		    column.setHeaderValue(columnNames[i]);
		}
		
		return getTableHeader();	
	}

	public Rule getRule() throws FirewallException{
		int j=0;
		Rule rule = new Rule();
		String str;
		str = (String) this.getValueAt(0, j++);
		try {
			rule.index    = Integer.parseInt(str);
		}catch(NumberFormatException e){
			throw  new FirewallException(    "Wrond data format:index is not a valid number");
		}
		
		
        str = (String) this.getValueAt(0, j++);;
        if (str==null ||  str.equals("")){
        	rule.iprange  = null;
        }else{
        	rule.iprange  = str;
        }
        
        str = (String) this.getValueAt(0, j++);
        try {
        	if (str==null ||  str.equals("")){
        		rule.port     = -1;
        	}
        	else{
        		rule.port =  Integer.parseInt(	str );
        	}
        }catch(NumberFormatException e){
			throw  new FirewallException(    "Wrond data format:index is not number");
		}
		str = (String) this.getValueAt(0, j++);
		
		if (str ==null ||  str.equals("")){
			rule.type = Rule.typeOpt.IP;
			rule.protocol = null;
		}
		else{
			rule.type = Rule.typeOpt.CONTEXT;
			rule.protocol = str;
			if ( rule.iprange != null){
				throw  new FirewallException(    "Wrond rule format:You can't use protocol and ip in one rule");
			}
		}
		
		
	    str = (String) this.getValueAt(0, j++);
		if (str.equals("DENY")){
			rule.permission = Rule.permissionOpt.DENY;
		}
		else if (str.equals("ALLOW") ){
			rule.permission = Rule.permissionOpt.ALLOW;
		}else{
			throw  new FirewallException(    "Wrond data format:action has to be \"ALLOW\" or \"DENY\"");
		}
		return  rule;
		
		
	}
}
